package com.business.file.controller;

import com.business.common.dto.R;
import com.business.common.enums.BusinessExceptionEnum;
import com.business.common.enums.ErrorEnum;
import com.business.common.utils.FileUtil;
import com.business.file.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/upload")
public class UploadController {

    @Autowired
    private FileService fileService;

    private Logger logger = LoggerFactory.getLogger(UploadController.class);


    /**
     * 上传多张图片
     *
     * @param fileList
     * @date 2019-04-28 15:03:18
     */
    @PostMapping(value = "/uploadImageList")
    @ResponseBody
    public R<String> uploadImageList(@RequestPart("file") MultipartFile[] fileList, @RequestParam(value = "module") String module, @RequestParam(value = "folder", required = false) String folder) {
        //先检查
        for (MultipartFile file : fileList) {
            try {
                if (file == null) {
                    return R.error(BusinessExceptionEnum.FILE_READING_ERROR, "文件名：" + file.getOriginalFilename());
                }
                if (!FileUtil.validateFileSize(file, 100)) {//图片大小限制
                    return R.error(BusinessExceptionEnum.OVER_SIZE_FILE, "文件名：" + file.getOriginalFilename());
                }
                if (FileUtil.getType(file) == null) {//类型不对
                    return R.error(BusinessExceptionEnum.IMAGE_TYPE_ERROR, "文件名：" + file.getOriginalFilename());
                }
            } catch (IOException e) {
                logger.error("上传图片失败," + e.getMessage(), e);
                return R.error(ErrorEnum.UNKNOWN_ERROR);
            }
        }
        List<String> fullFileNameList = new ArrayList<>();
        for (MultipartFile file : fileList) {
            R<String> r = fileService.uploadImage(file, module, folder);
            fullFileNameList.add(r.getData());
        }
        return R.success(fullFileNameList);

    }


    /**
     * 上传图片
     *
     * @param fileUpload
     * @date 2019-04-28 15:03:18
     */
    @PostMapping(value = "/uploadImage")
    @ResponseBody
    public R<String> uploadImage(@RequestPart("file") MultipartFile fileUpload, @RequestParam(value = "module") String module, @RequestParam(value = "folder", required = false) String folder) {
        try {
            if (fileUpload == null) {
                return R.error(BusinessExceptionEnum.FILE_READING_ERROR);
            }
            if (!FileUtil.validateFileSize(fileUpload, 100)) {//图片大小限制
                return R.error(BusinessExceptionEnum.OVER_SIZE_FILE);
            }
            if (FileUtil.getType(fileUpload) == null) {//类型不对
                return R.error(BusinessExceptionEnum.IMAGE_TYPE_ERROR);
            }
        } catch (IOException e) {
            logger.error("上传图片失败," + e.getMessage(), e);
        }
        return fileService.uploadImage(fileUpload, module, folder);
    }

    /**
     * 删除图片
     *
     * @date 2019-04-28 15:03:18
     */
    @PostMapping("/deleteImage")
    @ResponseBody
    public R<String> deleteImage(@RequestParam("filePath") String filePath) {
        return fileService.delImage(filePath);
    }

    /**
     * 批量删除图片
     *
     * @date 2019-04-28 15:03:18
     */
    @PostMapping("/deleteImageList")
    @ResponseBody
    public R<String> deleteImageList(@RequestParam("filePathList") String[] filePathList) {
        for (String url : filePathList) {
            fileService.delImage(url);
        }
        return R.success();
    }


    /**
     * 删除目录
     *
     * @date 2019-04-28 15:03:18
     */
    @PostMapping("/delFolder")
    @ResponseBody
    public R<String> delFolder(@RequestParam("filePath") String filePath) {
        return fileService.delFolder(filePath);
    }
}
